
import { formatTime } from '../../utils/util.js'
import { addOrder,getOrderShare } from '../../api/order'
// pages/sharePage/sharePage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailData: {},
    endTime: '',
    showBtn: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let opOrder=JSON.parse(options.value)
    console.log(opOrder,'opOrder-------1')
    getOrderShare(opOrder).then(res => {
      console.log(res.data,'res----------2')
      opOrder.preOrderSn=res.data.preOrderSn
      opOrder.order_splicing_sn=res.data.order_splicing_sn
      
      this.setData({
        detailData:opOrder,
        endTime: formatTime(opOrder.splicingEndTime)
      })
    })
  },
  applyNow() {
    console.log(this.data.detailData.preOrderSn,'this.data.detailData.preOrderSn')
    let now = Math.floor(new Date().getTime() / 1000);
    if (now > this.data.detailData.splicingEndTime) {
      wx.showToast({
        title: '已超出拼单时效！',
        icon: 'none'
      })
      return
    }
    let params = {
      "order_sn": this.data.detailData.preOrderSn,
      "order_splicing_sn": this.data.detailData.order_splicing_sn,
      "model_user_id": this.data.detailData.model_user_id,
      "use_time": this.data.detailData.use_time,
      "user_duration": this.data.detailData.user_duration,
      "site_id": this.data.detailData.site_id,
      "use_site_time": this.data.detailData.use_site_time,
      "has_site": this.data.detailData.has_site,
    }
    addOrder(params).then(res => {
      this._paysuccess(res)
    })
  },
  _paysuccess(res){
    let that=this
    wx.requestPayment({
      timeStamp: res.data.timeStamp,
      nonceStr: res.data.nonceStr,
      package: res.data.package,
      signType: res.data.signType,
      paySign: res.data.paySign,
      success() {
        wx.showToast({
          title: '拼单成功',
          icon: 'none'
        })
        that.setData({
            showBtn: false
        })
      },
      fail() {
        wx.showToast({
          title: '购买失败！',
          icon: 'error'
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})